using System;
using System.Data;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using CatalogSystemProxy;
using System.Xml;

namespace CatalogManager.CategoryForms
{
	/// <summary>
	/// Summary description for CategoryDefintion.
	/// </summary>
	public class CategoryDefintion : System.Windows.Forms.Form
	{
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.Button btnContinue;
		private System.Windows.Forms.Button btnCancel;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;
		private System.Windows.Forms.ListBox listBoxCategoryDefintions;

		private CatalogSystem CSService = null;
		public string SelectCategoryDef=null;

		public CategoryDefintion()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			//
			// TODO: Add any constructor code after InitializeComponent call
			//
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.label1 = new System.Windows.Forms.Label();
			this.listBoxCategoryDefintions = new System.Windows.Forms.ListBox();
			this.btnContinue = new System.Windows.Forms.Button();
			this.btnCancel = new System.Windows.Forms.Button();
			this.SuspendLayout();
			// 
			// label1
			// 
			this.label1.Location = new System.Drawing.Point(8, 16);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(272, 23);
			this.label1.TabIndex = 0;
			this.label1.Text = "Select a Category definiton type:";
			// 
			// listBoxCategoryDefintions
			// 
			this.listBoxCategoryDefintions.Location = new System.Drawing.Point(8, 48);
			this.listBoxCategoryDefintions.Name = "listBoxCategoryDefintions";
			this.listBoxCategoryDefintions.Size = new System.Drawing.Size(272, 173);
			this.listBoxCategoryDefintions.TabIndex = 1;
			// 
			// btnContinue
			// 
			this.btnContinue.Location = new System.Drawing.Point(72, 232);
			this.btnContinue.Name = "btnContinue";
			this.btnContinue.TabIndex = 2;
			this.btnContinue.Text = "Continue";
			this.btnContinue.Click += new System.EventHandler(this.btnContinue_Click);
			// 
			// btnCancel
			// 
			this.btnCancel.Location = new System.Drawing.Point(152, 232);
			this.btnCancel.Name = "btnCancel";
			this.btnCancel.TabIndex = 3;
			this.btnCancel.Text = "Cancel";
			this.btnCancel.Click += new System.EventHandler(this.btnCancel_Click);
			// 
			// CategoryDefintion
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(288, 266);
			this.Controls.AddRange(new System.Windows.Forms.Control[] {
																		  this.btnCancel,
																		  this.btnContinue,
																		  this.listBoxCategoryDefintions,
																		  this.label1});
			this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
			this.MaximizeBox = false;
			this.Name = "CategoryDefintion";
			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
			this.Text = "CategoryDefintion";
			this.Load += new System.EventHandler(this.CategoryDefintion_Load);
			this.ResumeLayout(false);

		}
		#endregion

		private void CategoryDefintion_Load(object sender, System.EventArgs e)
		{
			// read the xml file and get the file path
			XmlDocument doc = new XmlDocument();
			doc.Load("SiteConfig.xml");
			CSService = new CatalogSystem(doc.FirstChild.InnerText);
			// load category definitions
			// reset the local variables
			SelectCategoryDef=null;

			// get all definitons
			DataSet ds = CSService.GeCategoryDefintions();

			// load defintions
			foreach(DataRow dr in ds.Tables[0].Rows)
			{
				listBoxCategoryDefintions.Items.Add(dr["DefinitionName"]);
			}
		}

		private void btnCancel_Click(object sender, System.EventArgs e)
		{
			// exit
			this.Close();
		}

		private void btnContinue_Click(object sender, System.EventArgs e)
		{
			// get the selected item
			SelectCategoryDef = (string)listBoxCategoryDefintions.SelectedItem;
			this.Close();
		}
	}
}
